﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Models;
using AnesSystem.ViewModels.WorkScore;
using Common;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ExcelDll = Microsoft.Office.Interop.Excel;
using AnesSystem.Services;

namespace AnesSystem.Util
{
    public class ExcelBasic
    {
        #region Variables

        public static DateTime[] months = new DateTime[12];
        #endregion

        #region Constructor

        #endregion

        #region Properties

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods
        public void getMonthsDates(string year)
        {
            Array.Clear(months, 0, months.Length);
            for (int i = 1; i < 13; i++)
            {
                months[i - 1] = Convert.ToDateTime(year + "-" + i + "-1");
            }
        }

        //单元格样式
        public static ExcelDll.Worksheet merge(ExcelDll.Worksheet worksheet, string text, int x1, int y1, int x2, int y2)
        {
            worksheet.Range[worksheet.Cells[x1, y1], worksheet.Cells[x2, y2]].Merge(worksheet.Range[worksheet.Cells[x1, y1], worksheet.Cells[x2, y2]].MergeCells);
            worksheet.Cells[x1, y1] = text;
            worksheet.Cells[x1, y1].Font.Bold = true;
            worksheet.Cells[x1, y1].HorizontalAlignment = ExcelDll.XlHAlign.xlHAlignCenter;
            worksheet.Cells[x1, y1].EntireColumn.AutoFit();
            return worksheet;
        }
        //根据时间,医生姓名获取手术信息
        private static List<PatientLinkModel> getPatientLinkDate(DateTime startDate, DateTime endDate, string accountName)
        {
            List<PatientLinkModel> patientLinkList = PatientLinkService.Instance.GetFinishOperByTime(startDate, endDate);
            List<AccountModel> selAccountList = AccountService.Instance.findAccountListByName(accountName);
            List<string> accountIdList = new List<string>();
            if (null != selAccountList && selAccountList.Count() > 0)
            {
                for (int i = 0; i < selAccountList.Count(); i++)
                {
                    accountIdList.Add(selAccountList[i].Id.ToString());
                }
            }
            List<PatientLinkModel> patientLinks = new List<PatientLinkModel>();
            if (null != accountIdList && accountIdList.Count() > 0)
            {
                for (int i = 0; i < patientLinkList.Count; i++)
                {
                    for (int j = 0; j < accountIdList.Count; j++)
                    {
                        if (isOperOfAllDoctor(patientLinkList[i], accountIdList[j]))
                        {
                            patientLinks.Add(patientLinkList[i]);
                        }
                    }
                }
            }
            else
            {
                patientLinks.AddRange(patientLinkList);
            }
            patientLinkList.OrderBy(u => u.Status.InRoomTime).ToList();
            return patientLinkList;
        }

        //已施手术合并
        private static string getYiShiOper(PatientLinkModel patientLink)
        {
            string nishiOper = "";
            if (null != patientLink.AnesthesiaRecord.Ysss1 && !"".Equals(patientLink.AnesthesiaRecord.Ysss1) &&
                    null != patientLink.AnesthesiaRecord.Ysss2 && !"".Equals(patientLink.AnesthesiaRecord.Ysss2))
                nishiOper += ("①" + patientLink.AnesthesiaRecord.Ysss1);
            else if (null != patientLink.AnesthesiaRecord.Ysss1 && !"".Equals(patientLink.AnesthesiaRecord.Ysss1) &&
                    null == patientLink.AnesthesiaRecord.Ysss2 || "".Equals(patientLink.AnesthesiaRecord.Ysss2))
            {
                nishiOper += (patientLink.AnesthesiaRecord.Ysss1);
            }
            else return "";
            if (null != patientLink.AnesthesiaRecord.Ysss2 && !"".Equals(patientLink.AnesthesiaRecord.Ysss2))
                nishiOper += (" ②" + patientLink.AnesthesiaRecord.Ysss2);
            if (null != patientLink.AnesthesiaRecord.Ysss3 && !"".Equals(patientLink.AnesthesiaRecord.Ysss3))
                nishiOper += (" ③" + patientLink.AnesthesiaRecord.Ysss3);
            if (null != patientLink.AnesthesiaRecord.Ysss4 && !"".Equals(patientLink.AnesthesiaRecord.Ysss4))
                nishiOper += (" ④" + patientLink.AnesthesiaRecord.Ysss4);
            return nishiOper;
        }
        //拟施手术合并
        private static string getNiShiOper(PatientLinkModel patientLink)
        {
            string nishiOper = "";
            if (null != patientLink.AnesthesiaRecord.Nsss1 && !"".Equals(patientLink.AnesthesiaRecord.Nsss1) &&
                    null != patientLink.AnesthesiaRecord.Nsss2 && !"".Equals(patientLink.AnesthesiaRecord.Nsss2))
                nishiOper += ("①" + patientLink.AnesthesiaRecord.Nsss1);
            else if (null != patientLink.AnesthesiaRecord.Nsss1 && !"".Equals(patientLink.AnesthesiaRecord.Nsss1) &&
                    null == patientLink.AnesthesiaRecord.Nsss2 || "".Equals(patientLink.AnesthesiaRecord.Nsss2))
            {
                nishiOper += (patientLink.AnesthesiaRecord.Nsss1);
            }
            else return "";
            if (null != patientLink.AnesthesiaRecord.Nsss2 && !"".Equals(patientLink.AnesthesiaRecord.Nsss2))
                nishiOper += (" ②" + patientLink.AnesthesiaRecord.Nsss2);
            if (null != patientLink.AnesthesiaRecord.Nsss3 && !"".Equals(patientLink.AnesthesiaRecord.Nsss3))
                nishiOper += (" ③" + patientLink.AnesthesiaRecord.Nsss3);
            if (null != patientLink.AnesthesiaRecord.Nsss4 && !"".Equals(patientLink.AnesthesiaRecord.Nsss4))
                nishiOper += (" ④" + patientLink.AnesthesiaRecord.Nsss4);
            return nishiOper;
        }
        //医生名称转换
        private static bool isOperOfAllDoctor(PatientLinkModel patientLink, string accountID)
        {
            try
            {
                AnesthesiaRecordModel anesthesiaRecord = patientLink.AnesthesiaRecord;
                if (accountID.Equals(anesthesiaRecord.Zmys))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.Fmys1))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.Fmys2))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.Fmys3))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.Mzjbys))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.Xhhs1))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.Xhhs2))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.Xhhs3))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.Xshs1))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.Xshs2))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.Xshs3))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.OperDoctor1))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.OperDoctor2))
                {
                    return true;
                }
                else if (accountID.Equals(anesthesiaRecord.OperDoctor3))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public static long getTimeSpan(DateTime d1, DateTime d2)
        {
            long spanTime = 0;
            TimeSpan span = d2 - d1;
            long Minutes = span.Days * 24 * 60 + span.Hours * 60 + span.Minutes;
            if (Minutes > 0)
            {
                spanTime = Minutes;
            }
            return spanTime;
        }

        #endregion

        #region Public/Protected Methods
        //导出主方法
        public void ExportParameterCommon(string titleName, DateTime startDate, DateTime endDate, string accountName)
        {

            string fileName = titleName;
            string saveFileName = titleName;
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xlsx";
            saveDialog.Filter = "Excel 文件|*.xlsx";
            saveDialog.FileName = fileName;
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return;  //被点了取消
            ExcelDll.Application xlApp = new ExcelDll.Application();
            if (xlApp == null)
            {
                MessageBoxCommon.ShowDialog("无法创建Excel对象，您可能未安装Excel!", MessageBoxCommon.OK);
                return;
            }
            ExcelDll.Workbooks workbooks = xlApp.Workbooks;
            ExcelDll.Workbook workbook = workbooks.Add(ExcelDll.XlWBATemplate.xlWBATWorksheet);
            ExcelDll.Worksheet worksheet = (ExcelDll.Worksheet)workbook.Worksheets[1]; //取得sheet1
            if (titleName.Contains("质量检测"))
            {
                List<QualityStatisticsModel> QIDataList = QIDataConversion();
                worksheet = QIWorkbookProduction(fileName, worksheet, QIDataList);
            }
            else if (titleName.Contains("工作量"))
            {
                List<PatientLinkModel> patientLinkList = getPatientLinkDate(startDate, endDate, accountName);
                Dictionary<int, Dictionary<int, string>> rowMapColData = QWDataConversion(fileName, patientLinkList, accountName);
                worksheet = QWWorkbookProduction(fileName, worksheet, rowMapColData);
            }
            else if (titleName.Contains("麻醉量化分登记"))
            {

            }
            else
            {

            }

            //写入行
            worksheet.Columns.EntireColumn.AutoFit();
            //MessageBoxCommon.ShowDialog(fileName + "保存成功", MessageBoxCommon.OK);
            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                }
                catch (Exception ex)
                {
                    MessageBoxCommon.ShowDialog("保存成功导出文件可能正在被打断", MessageBoxCommon.OK);
                }

            }
            xlApp.Quit();
            GC.Collect();
        }

        public void ExportParameterCommon(string titleName, DataGrid dataGrid)
        {

            string fileName = titleName;
            string saveFileName = titleName;
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xlsx";
            saveDialog.Filter = "Excel 文件|*.xlsx";
            saveDialog.FileName = fileName;
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0) return;  //被点了取消
            ExcelDll.Application xlApp = new ExcelDll.Application();
            if (xlApp == null)
            {
                MessageBoxCommon.ShowDialog("无法创建Excel对象，您可能未安装Excel!", MessageBoxCommon.OK);
                return;
            }
            ExcelDll.Workbooks workbooks = xlApp.Workbooks;
            ExcelDll.Workbook workbook = workbooks.Add(ExcelDll.XlWBATemplate.xlWBATWorksheet);
            ExcelDll.Worksheet worksheet = (ExcelDll.Worksheet)workbook.Worksheets[1]; //取得sheet1

            worksheet = DataGridWorkbookProduction(fileName, worksheet, dataGrid);


            //写入行
            worksheet.Columns.EntireColumn.AutoFit();
            //MessageBoxCommon.ShowDialog(fileName + "保存成功", MessageBoxCommon.OK);
            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                }
                catch (Exception ex)
                {
                    MessageBoxCommon.ShowDialog("保存成功导出文件可能正在被打断", MessageBoxCommon.OK);
                }

            }
            xlApp.Quit();
            GC.Collect();
        }


        protected virtual string[] GetQITiltles()//质控标题中文
        {
            string[] value = { "总麻醉数", "术后镇痛", "心肺复苏数", "复苏成功数", "麻醉复苏室列数", "Asa一级", "Asa二级", "Asa三级", "Asa四级", "Asa五级", "Asa六级", "急诊", "神经阻滞", "椎管内麻醉", "全麻", "复合麻醉", "静脉麻醉例数", "其它麻醉", "麻醉后手术取消例数", "PACU转出延迟", "非计划转入ICU发生例数", "非计划二次插管例数", "围手术期死亡例数", "Steward评分≥4数", "未预期意识障碍数", "氧饱和度重度降低数", "多次插管数", "多次穿刺数", "麻醉期间严重过敏反应发生例数", "椎管内麻醉并发症", "中心静脉穿刺严重并发症发生", "全麻气管插管后声嘶发生例数", "麻醉效果1", "麻醉效果2", "麻醉效果3", "麻醉效果4", "围手术期心跳骤停例数", "中心静脉穿刺数", "静脉穿刺数", "桡动脉穿刺数", "麻醉手术后新发昏迷发生例数", "非计划二次手术数", "非计划改变麻醉方式数", "非计划二次麻醉数", "术中输血输液反应数", "红细胞回输例数", "意外死亡数", "结束时使用催醒药物数", "其他非预期相关事件数", "总手术数", "手术分类1", "手术分类2", "手术分类3", "手术分类4", "浅层组织手术", "深部组织手术", "器官手术", "腔隙内手术", "体外循环", "术后气管插管拔除", "转入ICU", "转入PACU", "静脉PCA，PCIA", "皮下PCA，PCASA", "硬膜外，PCEA", "神经丛，PCNA", "锁骨上静脉穿刺数", "股静脉穿刺数", "足背动脉穿刺数", "麻醉后头痛数", "穿刺点红肿数", "锁骨下静脉穿刺数", "恶心呕吐数", "肢体障碍数", "重大手术数", "控制性降压", "穿破硬脊膜", "术中知晓", "术中低血压", "有创动脉穿刺并发", "用血数", "用血合适例数", "更改手术方式", "手术室医疗干预", "急性失血", "剖宫产手术时间大于3小时", "镇痛分娩转剖宫产", "非手术范围器官损伤", "超声引导", "动脉穿刺", "使用输注工作站", "血液加温治疗" };
            return value;
        }
        protected virtual string[] GetQIMethods()//质控标题对应字段
        {
            string[] methods = { "MaZuiCount", "ShuHouZhenTong", "XinFeiFuSu", "FuSuChengGong", "PACUCount", "ASA1", "ASA2", "ASA3", "ASA4", "ASA5", "ASA6", "JiZhen", "ShenJingZuZhi", "ZhuiGuanNeiMaZauiCount", "ChaGuanQuanMaCount", "FuHeMaZuiCount", "JingMaiMaZuiConut", "QiTaMaZuiCount", "CancelShouShu", "ZhuanChuYanChi", "ZhuanRuICU", "ErCiChaGuan", "SIWang", "StewardBig4", "WeiYuQiYiShi", "Spo2JiangDi", "DuoCiChaGuan", "DuoCiChuanCi", "GuoMinYanZhong", "ShenJingBingFa", "JingMaiChuanCiBingFa", "QiGuanShengSi", "QuanMaXiaoGuo1", "QuanMaXiaoGuo2", "QuanMaXiaoGuo3", "QuanMaXiaoGuo4", "XinTiaoStop", "ZhongXinJingMaiChuanCi", "JingNeiJingMaiChuanCi", "NaoDongMaiChuanCi", "HunMi", "NotPlan2ShouShu", "NotPlan2MazuiFangShi", "NotPlan2Mazui", "ShuZhongShuXueShuYeFY", "HongXiBaoHuiShu", "YiWaiDead", "UseCuiXingDrug", "OtherFeiYuQiShiJian", "PatientLinkCount", "OperType1", "OperType2", "OperType3", "OperType4", "QianCengZuZhi", "ShenBuZuZhi", "QiGuan", "QiangXiNei", "TiWaiXunHuan", "QiGuanBaChuCount", "ICUCount", "PACUCount", "JingMaiPCA", "PiXiaPCA", "YingMoWaiPCA", "ShengJingCongPCA", "SuoGuShangJingMaiChuanCi", "GuJingMaiChuanCi", "ZuBeiDongMaiChuanCi", "MazuiHouTouTong", "ChuanCiDianHongZong", "SuoGuXiaJingMaiChuanCi", "EXinOuTu", "ZhiTiZangAi", "ZhongDaShouShu", "KongZhiXingJiangYa", "ChuanPoYingJiMo", "ShuZhongZhiXiao", "ShuZhongDiXueYa", "YouChuangDongMaiCCBingFa", "YongXueShu", "YongXueHeLiShu", "GengGaiShouShuMethod", "ShouShuShiYiLiaoGanYu", "JiXingShiXue", "PouGongChanShouShu", "ZhenTongFenZhuanPgc", "NotShouShuFanWeiQiGuanSunShang", "ChaoShengYinDao", "DongMaiChuanCi", "ShiYongShuZhuGongZuoZhan", "XueYeJiaWenZhiLiao" };
            return methods;
        }
        protected virtual string[] GetQWHeaders1()//质控标题对应字段
        {
            string[] header1 = new string[] { "日期", "姓名", "性别", "年龄", "科别+床号", "住院号", "手术名称", "ASA\n分级", "急诊", "特殊情况", "麻醉医生", "查对" };
            return header1;
        }

        protected virtual string[] GetQWHeaders2()//质控标题对应字段
        {
            string[] headerMazuiMethod = new string[] { "普通\n插管\n全麻", "单腔\n管全\n麻", "双腔\n管全\n麻", "喉罩\n插管\n全麻", "异型\n管全\n麻", "硬外\n膜", "腰麻", "腰硬\n联合", "神经\n阻滞", "静脉\n全麻", "局麻", "复合\n麻醉", "静\n脉", "动\n脉", "静\n脉", "硬\n外\n膜" };

            return headerMazuiMethod;
        }
        protected virtual string[] GetQWHeaders3()//质控标题对应字段
        {
            string[] headerQuXiang = new string[] { "PACU", "病\n房", "ICU", "重大\n手术", "非计划\n二次手\n术", "非计划\n二次插\n管", "非计划\n改变麻\n醉方式", "非计划\n二次麻\n醉", "术中输\n血、输\n液反应", "术后意\n识障碍", "术后氧\n饱和度\n重度降\n低", "误吸所\n致呼吸\n道梗阻", "术中、\n术后心\n肺复苏" };
            return headerQuXiang;
        }

        //质控数据转换
        protected virtual List<QualityStatisticsModel> QIDataConversion()
        {
            List<QualityMessageModel> QualityMessageList = QualityMessageService.Instance.GetAllQualityMessageModelList().ToList();
            QualityMessageList = QualityMessageList.OrderBy(o => o.LastUpdateTime).ToList();
            List<QualityStatisticsModel> resultList = QualityMessageList[QualityMessageList.Count - 1].ResultList;
            return resultList;
        }
        //质控excel填值
        protected virtual ExcelDll.Worksheet QIWorkbookProduction(string fileName, ExcelDll.Worksheet worksheet, List<QualityStatisticsModel> QIDataList)
        {
            ExcelDll.Range excelRange = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[2, 16]];

            //设置字体大小
            excelRange.Font.Size = 20;
            excelRange.Font.Bold = true;

            //设置字体在单元格内的对其方式
            excelRange.HorizontalAlignment = ExcelDll.XlHAlign.xlHAlignCenter;
            //自动调整列宽
            excelRange.EntireColumn.AutoFit();
            //文本自动换行
            excelRange.WrapText = true;

            //合并单元格
            excelRange.Merge(excelRange.MergeCells);
            worksheet.Cells[1, 1] = fileName;
            worksheet.Cells[3, 1] = "指标名称/日期";

            for (int i = 1; i < 13; i++)
            {
                worksheet.Cells[3, i + 1] = i + "月";
            }
            if (QIDataList.Count > 0)
            {

                getMonthsDates(QIDataList[0].Date.Year.ToString());

                for (int i = 0; i < GetQITiltles().Length; i++)
                {
                    worksheet.Cells[i + 4, 1] = GetQITiltles()[i];
                    for (int j = 0; j < months.Length; j++)
                    {
                        foreach (var item in QIDataList)
                        {
                            if (item.Date == months[j])
                            {
                                var propertyInfo = item.GetType().GetProperty(GetQIMethods()[i]);
                                if (propertyInfo != null)
                                {
                                    var value = propertyInfo.GetValue(item, null);
                                    worksheet.Cells[i + 4, j + 2] = value;
                                }
                            }
                        }
                    }
                }
            }

            return worksheet;
        }

        //工作量数据转换
        protected virtual Dictionary<int, Dictionary<int, string>> QWDataConversion(string fileName, List<PatientLinkModel> patientLinkList, string accountName)
        {
            Dictionary<int, Dictionary<int, string>> rowMapColData = new Dictionary<int, Dictionary<int, string>>();

            if (null == patientLinkList || patientLinkList.Count == 0) return rowMapColData;
            patientLinkList = patientLinkList.OrderBy(O => O.Status.InRoomTime).ToList();
            int row = 0;
            for (int i = 0; i < patientLinkList.Count; i++)
            {
                //System.out.println("现在是第" + i);
                Dictionary<int, string> colMapData = new Dictionary<int, string>();
                PatientLinkModel patientLink = patientLinkList[i];
                string patName = patientLink.Patient.Brxm == null ? "" : patientLink.Patient.Brxm;
                if (patName.Length == 0) continue;

                DateTime inRoomTime = patientLink.Status.InRoomTime;
                string inRoomTimeStr = null == inRoomTime ? "" : inRoomTime.ToString("yyyy-MM-dd");
                colMapData.Add(0, inRoomTimeStr);

                //病人姓名
                colMapData.Add(1, patName);

                //病人性别
                string patSex = patientLink.Patient.Xb == null ? "" : patientLink.Patient.Xb;
                colMapData.Add(2, patSex);

                colMapData.Add(3, patientLink.Patient.Nl);

                //床号
                string deptName = patientLink.Patient.Ksmc == null ? "" : patientLink.Patient.Ksmc;
                string bed = patientLink.Patient.Zych == null ? "" : patientLink.Patient.Zych;
                colMapData.Add(4, deptName + "-" + bed);

                //住院号
                string zyh = patientLink.Patient.Zyh == null ? "" : patientLink.Patient.Zyh;
                colMapData.Add(5, zyh);

                //拟施手术
                string operName = getYiShiOper(patientLink);
                if (operName.Length == 0) operName = getNiShiOper(patientLink);
                colMapData.Add(6, operName);

                //ASA分级
                string asaLevel = patientLink.AnesthesiaRecord.Asa.Level == null ? "" : patientLink.AnesthesiaRecord.Asa.Level;
                colMapData.Add(7, asaLevel);

                //急诊
                if (patientLink.AnesthesiaRecord.Asa.Jz)
                {
                    colMapData.Add(8, "1");
                }

                //麻醉方式
                string complicatedMethod = patientLink.AnesthesiaRecord.Mzff;
                if (complicatedMethod.Contains("气管插管全麻"))
                {
                    colMapData.Add(9, "1");
                }
                if (complicatedMethod.Contains("异型管全麻"))
                {
                    colMapData.Add(13, "1");
                }
                if (complicatedMethod.Contains("硬膜外"))
                {
                    colMapData.Add(14, "1");
                }
                if (complicatedMethod.Contains("腰麻"))
                {
                    colMapData.Add(15, "1");
                }
                if (complicatedMethod.Contains("腰硬联合"))
                {
                    colMapData.Add(16, "1");
                }
                if (complicatedMethod.Contains("神经阻滞"))
                {
                    colMapData.Add(17, "1");
                }
                if (complicatedMethod.Equals("静脉麻醉"))
                {
                    colMapData.Add(18, "1");
                }
                if (complicatedMethod.Contains("局麻"))
                {
                    colMapData.Add(19, "1");
                }
                if (complicatedMethod.Contains("复合麻醉"))
                {
                    colMapData.Add(20, "1");
                }

                //血管穿刺
                if (patientLink.AnesthesiaRecord.Zxjmcc)
                {
                    colMapData.Add(21, "1");
                }
                if (patientLink.AnesthesiaRecord.Dmcc)
                {
                    colMapData.Add(22, "1");
                }
                //镇痛泵
                if (patientLink.AnesthesiaRecord.PCIA)
                {
                    colMapData.Add(23, "√");
                }
                if (patientLink.AnesthesiaRecord.PCEA)
                {
                    colMapData.Add(24, "√");
                }

                //病人去向
                string quXiang = patientLink.AnesthesiaRecord.Brqw;
                if (null != quXiang)
                {
                    if (quXiang.Equals("复苏室") || quXiang.Equals("PACU"))
                    {
                        colMapData.Add(26, "√");
                    }
                    else if (quXiang.Equals("病房"))
                    {
                        colMapData.Add(27, "√");
                    }
                    else if (quXiang.Equals("ICU"))
                    {
                        colMapData.Add(28, "√");
                    }
                }
                QualityPojoModel qualityPojo = patientLink.QualityPojo;
                //质控指标
                if (qualityPojo.Zdss)
                {
                    colMapData.Add(29, "√");
                }
                if (qualityPojo.Fjhecss)
                {
                    colMapData.Add(30, "√");
                }
                if (qualityPojo.Fjhecqgbg)
                {
                    colMapData.Add(31, "√");
                }
                if (qualityPojo.Fjhgbmzfs)
                {
                    colMapData.Add(32, "√");
                }
                if (qualityPojo.Fjhecmz)
                {
                    colMapData.Add(33, "√");
                }
                if (qualityPojo.Szsxsyfy)
                {
                    colMapData.Add(34, "√");
                }
                if (qualityPojo.Wyqysza)
                {
                    colMapData.Add(35, "√");
                }
                if (qualityPojo.Ybhhdzdjd)
                {
                    colMapData.Add(36, "√");
                }
                if (false)
                {
                    colMapData.Add(37, "√");
                }
                if (false)
                {
                    colMapData.Add(38, "√");
                }

                //麻醉医生
                string mazuiDoc = patientLink.AnesthesiaRecord.Zmys == null ? "" : DataManager.Instance.GetAccountNameByID(patientLink.AnesthesiaRecord.Zmys);
                string mazuiDoc2 = patientLink.AnesthesiaRecord.Fmys1 == null ? "" : DataManager.Instance.GetAccountNameByID(patientLink.AnesthesiaRecord.Fmys1);
                string mazuiDoc3 = patientLink.AnesthesiaRecord.Fmys2 == null ? "" : DataManager.Instance.GetAccountNameByID(patientLink.AnesthesiaRecord.Fmys2);
                mazuiDoc = mazuiDoc + " " + mazuiDoc2 + " " + mazuiDoc3;
                colMapData.Add(39, mazuiDoc.Trim());

                //            //入手术室时间
                if (patientLink.Status.InRoomTime == null)
                {
                    colMapData.Add(41, "无");
                }
                else
                {
                    colMapData.Add(41, patientLink.Status.InRoomTime.ToString("yyyy-MM-dd HH:mm"));
                }

                //出手术室时间
                if (patientLink.Status.OutRoomTime == null)
                {
                    colMapData.Add(42, "无");
                }
                else
                {

                    colMapData.Add(42, patientLink.Status.OutRoomTime.ToString("yyyy-MM-dd HH:mm"));
                }

                //入室时长
                long totalTime;
                if (patientLink.Status.OutRoomTime != null && patientLink.Status.InRoomTime != null)
                {
                    totalTime = getTimeSpan(patientLink.Status.InRoomTime, patientLink.Status.OutRoomTime);
                    //totalTime = (long)(patientLink.Status.OutRoomTime - patientLink.Status.InRoomTime).TotalSeconds / 1000 / 60;//分钟
                }
                else
                {
                    totalTime = 0;
                }
                colMapData.Add(43, totalTime.ToString());

                //手术开始时间
                if (patientLink.Status.OperStartTime == null)
                {
                    colMapData.Add(44, "无");
                }
                else
                {
                    colMapData.Add(44, patientLink.Status.OperStartTime.ToString("yyyy-MM-dd HH:mm"));
                }
                //手术结束时间
                if (patientLink.Status.OperEndTime == null)
                {
                    colMapData.Add(45, "无");
                }
                else
                {
                    colMapData.Add(45, patientLink.Status.OperEndTime.ToString("yyyy-MM-dd HH:mm"));
                }
                //            手术时长
                long operationTime = patientLink.Status.OperSpanTime;//分钟
                colMapData.Add(46, operationTime.ToString());

                rowMapColData.Add(row, colMapData);
                row++;
            }

            return rowMapColData;
        }
        //工作量excel填值
        protected virtual ExcelDll.Worksheet QWWorkbookProduction(string fileName, ExcelDll.Worksheet worksheet, Dictionary<int, Dictionary<int, string>> rowMapColData)
        {

            //Borders.LineStyle 单元格边框线
            ExcelDll.Range excelRange = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, 41]];

            //单元格边框线类型(线型,虚线型)
            //excelRange.Borders.LineStyle = 1;
            //excelRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous;
            //指定单元格下边框线粗细,和色彩
            //excelRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).Weight = Excel.XlBorderWeight.xlMedium;
            //excelRange.Borders.get_Item(Excel.XlBordersIndex.xlEdgeBottom).ColorIndex = 3;

            //设置字体大小
            excelRange.Font.Size = 20;
            excelRange.Font.Bold = true;
            //excelRange.Font.Color = new SolidColorBrush(Colors.Red);
            //excelRange.Font.ColorIndex = new SolidColorBrush(Colors.Red);
            //设置字体是否有下划线
            //excelRange.Font.Underline = true;

            //设置字体在单元格内的对其方式
            excelRange.HorizontalAlignment = ExcelDll.XlHAlign.xlHAlignCenter;
            //设置单元格的宽度
            //excelRange.ColumnWidth = 15;
            //设置单元格的背景色
            //excelRange.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb();
            //// 给单元格加边框
            //excelRange.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThick,
            //                                          Excel.XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
            //自动调整列宽
            excelRange.EntireColumn.AutoFit();
            //文本自动换行
            excelRange.WrapText = true;
            //填充颜色为淡紫色
            //excelRange.Interior.ColorIndex = 39;

            //合并单元格
            excelRange.Merge(excelRange.MergeCells);
            worksheet.Cells[1, 1] = fileName;
            for (int i = 0; i < 9; i++)//header标题合并单元格
            {
                worksheet = merge(worksheet, GetQWHeaders1()[i], 2, i + 1, 3, i + 1);
            }
            worksheet = merge(worksheet, "麻醉方式", 2, 10, 2, 21);
            worksheet = merge(worksheet, "血管穿刺", 2, 22, 2, 23);
            worksheet = merge(worksheet, "镇痛泵", 2, 24, 2, 25);
            worksheet = merge(worksheet, "特殊情况", 2, 26, 3, 26);
            worksheet = merge(worksheet, "病人去向", 2, 27, 2, 29);
            worksheet = merge(worksheet, "麻醉质控", 2, 30, 2, 39);
            worksheet = merge(worksheet, "麻醉医生", 2, 40, 3, 40);
            worksheet = merge(worksheet, "查对", 2, 41, 3, 41);
            worksheet = merge(worksheet, "入手术室时间", 2, 42, 3, 42);
            worksheet = merge(worksheet, "出手术室时间", 2, 43, 3, 43);
            worksheet = merge(worksheet, "入室时长", 2, 44, 3, 44);
            worksheet = merge(worksheet, "手术开始时间", 2, 45, 3, 45);
            worksheet = merge(worksheet, "手术结束时间", 2, 46, 3, 46);
            worksheet = merge(worksheet, "手术时长", 2, 47, 3, 47);
            for (int i = 0; i < GetQWHeaders2().Length; i++)//header标题合并单元格
            {
                worksheet.Cells[3, i + 10] = GetQWHeaders2()[i];
            }
            for (int i = 0; i < GetQWHeaders3().Length; i++)//header标题合并单元格
            {
                worksheet.Cells[3, i + 27] = GetQWHeaders3()[i];
            }

            for (int row = 0; row < rowMapColData.Count; row++)
            {
                Dictionary<int, string> colMapData = rowMapColData[row];
                for (int col = 0; col < 60; col++)
                {
                    if (colMapData.ContainsKey(col))
                    {
                        string data = colMapData[col];
                        if (null == data || data.Length == 0) continue;
                        //Label colData = new Label(col + 1, row + 3, data, wcf_cell);
                        worksheet.Cells[row + 4, col + 1] = data;
                    }
                }
            }

            return worksheet;
        }

        //历史手术
        public virtual bool ExportHosital(DateTime start, DateTime end)
        {
            return true;
        }
        //DataGridExcel填值
        protected virtual ExcelDll.Worksheet DataGridWorkbookProduction(string fileName, ExcelDll.Worksheet worksheet, DataGrid dataGrid)
        {
            ExcelDll.Range excelRange = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[2, 17]];

            //设置字体大小
            excelRange.Font.Size = 20;
            excelRange.Font.Bold = true;

            //设置字体在单元格内的对其方式
            excelRange.HorizontalAlignment = ExcelDll.XlHAlign.xlHAlignCenter;
            //自动调整列宽
            excelRange.EntireColumn.AutoFit();
            //文本自动换行
            excelRange.WrapText = true;

            //合并单元格
            excelRange.Merge(excelRange.MergeCells);
            worksheet.Cells[1, 1] = fileName;
            for (int i = 0; i < dataGrid.Columns.Count; i++)
            {
                worksheet.Cells[3, i + 1] = dataGrid.Columns[i].Header;
            }
            for (int r = 0; r < dataGrid.Items.Count; r++)
            {
                for (int i = 0; i < dataGrid.Columns.Count; i++)
                {
                    worksheet.Cells[r + 4, i + 1] = (dataGrid.Columns[i].GetCellContent(dataGrid.Items[r]) as TextBlock).Text;   //读取DataGrid某一行某一列的信息内容，与DataGridView不同的地方
                }
                System.Windows.Forms.Application.DoEvents();
            }


            return worksheet;
        }

        #endregion

        #region EventHandlers

        #endregion


    }
}
